//
//  OfficalManager.m
//  LOL
//
//  Created by lanou3g on 15/11/19.
//  Copyright © 2015年 syx. All rights reserved.
//

#import "OfficalManager.h"

@interface OfficalManager ()

@property (nonatomic,assign) NSUInteger index;

@end



@implementation OfficalManager

-(instancetype)init{
    self = [super init];
    if (self) {
        self.index = 1;
    }
    return self;
}

static OfficalManager * officalManager = nil;
+ (instancetype)officalManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        officalManager = [[OfficalManager alloc]init];
    });
    return officalManager;
}

// 解析数据获取内容
- (void)getOfficalWithURL:(NSString *)urlStr CompletionHandle:(void(^)(NSArray * sourceArr))handle{
    if (self.UPDrow == YES) {
        self.index = self.index + 1;
        NSString * string = [NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/%@_list_%ld.shtml",urlStr,self.index];
        NSURL * url = [NSURL URLWithString:string];
        NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (error) {
                NSLog(@"解析快看内容失败");
                return ;
            }
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray * listArr = dic[@"list"];
            NSMutableArray * array = [NSMutableArray new];
            for (NSDictionary * dic in listArr) {
                FastLookModel * model = [FastLookModel new];
                [model setValuesForKeysWithDictionary:dic];
                [array addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                handle(array);
            });
        }];
        [dataTask resume];

    }else {
        
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/%@_list_1.shtml",urlStr]];
    NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"解析快看内容失败");
            return ;
        }
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray * listArr = dic[@"list"];
        NSMutableArray * array = [NSMutableArray new];
        for (NSDictionary * dic in listArr) {
            FastLookModel * model = [FastLookModel new];
            [model setValuesForKeysWithDictionary:dic];
            [array addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            handle(array);
            });
        }];
        [dataTask resume];
    }
}



@end
